package com.jic.interaction.ChinaPost.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jic.interaction.ChinaPost.model.entity.CommonWeBankResponse;
import com.jic.interaction.ChinaPost.model.vo.request.DamagesPayRequest;
import com.jic.interaction.ChinaPost.model.vo.request.MemberInfoRequest;
import com.jic.interaction.ChinaPost.model.vo.request.QueryUserInfoRequest;
import com.jic.interaction.ChinaPost.model.vo.request.SelectPaymentRequest;
import com.jic.interaction.ChinaPost.model.vo.response.SelectPaymentResponse;
import com.jic.interaction.ChinaPost.model.vo.response.UserInfoResponse;
import com.jic.interaction.ChinaPost.service.MemberService;
import com.jic.interaction.common.enums.RedisKey;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.RedisService;
import com.jic.interaction.utils.SignUtils;
import com.jic.util.DateUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@Service
public class MemberServiceImpl implements MemberService {
    @Value("${route.value}")
    private String url;

    @Value("${wz.appid}")
    private String appid;

    @Value("${wz.secret}")
    private String secret;

    @Autowired
    private RedisService redisService;

    @Override
    public Long save(MemberInfoRequest request) throws InterruptedException, ExecutionException, IOException {

        String url=this.url+"/m1/dbs-front/user/openAccount";
        //TODO 必输校验
        //业务参数
        Map map = new HashMap();
        map.put("appId",request.getAppId());
        map.put("userId",request.getUserId());
        map.put("phoneNo",request.getPhoneNo());
        map.put("city",request.getCity());
        String s = HttpClientUtil.doRequestWczPost(url, map);
        return Long.parseLong(s);
    }

    @Override
    public Long update(MemberInfoRequest request) throws InterruptedException, ExecutionException, IOException {
        String url = this.url+"/m/dbs-front/user/updateUserActInfo";
        Map map = new HashMap();
        map.put("appId",request.getAppId());
        map.put("userId",request.getUserId());
        map.put("phoneNo",request.getPhoneNo());
        map.put("city",request.getCity());
        JSONObject jsonObject = JSONObject.fromObject(request);
        String s = HttpClientUtil.requestPath(url, map,jsonObject.toString());
        return null;
    }

    @Override
    public UserInfoResponse queryUserInfoById(QueryUserInfoRequest queryUserInfoRequest) {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String timestamp = String.valueOf(ts);
        param.put("appId",appid);
        param.put("userId",queryUserInfoRequest.getUserId());
        Map<String,Object> header=new HashMap<>();
        header.put("appId",appid);
        header.put("nonce",nonceStr);
        header.put("timestamp",timestamp);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, redisService.getValue(RedisKey.WZ_SING_TICKET));
        String resData = HttpClientUtil.doRequestWczPost("/m1/dbs-front/user/queryAccountInfo?appId=" + appid + "&nonce=" + nonceStr + "&timestamp=" + timestamp + "&sign=" + sign + "&version=1.0.0", param);
        if(StringUtils.isNotEmpty(resData)){
            UserInfoResponse userInfoResponse = JSONArray.parseObject(resData, UserInfoResponse.class);
            System.out.println(userInfoResponse);
        }
        return null;
    }

    @Override
    public CommonWeBankResponse userAddPayInfo(DamagesPayRequest damagesPayRequest) {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String timestamp = String.valueOf(ts);
        param.put("appId",appid);
        param.put("userId",damagesPayRequest.getUserId());
        param.put("orderNo",damagesPayRequest.getOrderNo());
        param.put("txnTime", DateUtil.getStringFromDate(new Date(),"yyyyMMddHHmmss"));
        param.put("amount",damagesPayRequest.getAmount());
        param.put("payAmount",damagesPayRequest.getPayAmount());
        Map<String,Object> header=new HashMap<>();
        header.put("appId",appid);
        header.put("nonce",nonceStr);
        header.put("timestamp",timestamp);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, redisService.getValue(RedisKey.WZ_SING_TICKET));
        String resData = HttpClientUtil.doRequestWczPost("/m1/dbs-front/compensationPay/pay?appId=" + appid + "&nonce=" + nonceStr + "&timestamp=" + timestamp + "&sign=" + sign + "&version=1.0.0", param);
        if(StringUtils.isNotEmpty(resData)){
            CommonWeBankResponse commonWeBankResponse = JSONArray.parseObject(resData, CommonWeBankResponse.class);
            System.out.println(commonWeBankResponse);
        }
        return null;
    }

    @Override
    public SelectPaymentResponse userAddPayInfoByOrderNo(SelectPaymentRequest selectPaymentRequest) {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String timestamp = String.valueOf(ts);
        param.put("appId",appid);
        param.put("userId",selectPaymentRequest.getUserId());
        param.put("transType",selectPaymentRequest.getTransType());
        param.put("orderNo",selectPaymentRequest.getOrderNo());
        Map<String,Object> header=new HashMap<>();
        header.put("appId",appid);
        header.put("nonce",nonceStr);
        header.put("timestamp",timestamp);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, redisService.getValue(RedisKey.WZ_SING_TICKET));
        String resData = HttpClientUtil.doRequestWczPost("/m1/dbs-front/compensationPay/query?appId=" + appid + "&nonce=" + nonceStr + "&timestamp=" + timestamp + "&sign=" + sign + "&version=1.0.0", param);
        if(StringUtils.isNotEmpty(resData)){
            SelectPaymentResponse selectPaymentResponse = JSONArray.parseObject(resData, SelectPaymentResponse.class);
            System.out.println(selectPaymentResponse);
        }
        return null;
    }

    public static void addUser() {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W6650122");
        param.put("userId","1234");
//        param.put("phoneNo","13564615517");
//        param.put("city","上海");
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, "x08KAGWNF5WYVQu9UMMM3uoX57vhdcTt1MYoz7Djv72lP27SDQX7zhNG1cOOn8rC");
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/user/queryAccountInfo?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        System.out.println(s);
    }

    public static void pay() {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W3408745");
        param.put("userId","1234");
        param.put("orderNo","5a0dffc27259480780d8362935efdc21");
        param.put("txnTime", DateUtil.getStringFromDate(new Date(),"yyyyMMddHHmmss"));
        param.put("amount","0.1");
        param.put("payAmount","0.1");
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, "x08KAGWNF5WYVQu9UMMM3uoX57vhdcTt1MYoz7Djv72lP27SDQX7zhNG1cOOn8rC");
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/compensationPay/pay?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        System.out.println(s);
    }

    public static void queryPay() {
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W3408745");
        param.put("userId","1234");
        param.put("orderNo","5a0dffc27259480780d8362935efdc20");
        param.put("transType","P");
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, "x08KAGWNF5WYVQu9UMMM3uoX57vhdcTt1MYoz7Djv72lP27SDQX7zhNG1cOOn8rC");
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/compensationPay/query?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        System.out.println(s);
    }

    public static void main(String[] args) {
          //addUser();
          pay();
        // queryPay();
    }
}
